$(function(){
		var personnel = JSON.parse(sessionStorage.getItem("personnel"))//读取数据语法：====获取数据并将数据装换成json格式
		var type = JSON.parse(sessionStorage.getItem("type"))
		layui.use('form', function () {
			var form = layui.form;
			form.render();
			if(type==1){
				findByCompetence(form)
			}else{
				findByPositionId(form)
			}
		});
		$("#update").click(function(){
			var competenceId=$("#updatName option:selected").val();  //获取选中的项
			if(type==1){
				var positionId= findCompetenceByName(personnel.positionName)
			}else{
				var positionId= $("#updatName option:selected").val()
			}
			console.log(positionId)
			$.ajax({
				  url: "http://localhost:8081/personnel/updatePersonnel",//请求地址
				  type: "POST",//可以用post或者get
				  accept:"application/json",
				  dataType:"json",
				  data: {
					"personnelId": personnel.personnelId,
					"competenceId": competenceId,
					"personnelGrade": personnel.personnelGrade,
					"positionId": positionId
				  },//请求数据
				  success:function(data){
					  if(data.code==200){
						  layer.msg("修改成功", {icon: 6, time: 1000});
					  }else{
						   layer.msg("请求失败", {icon: 5, time: 1000});
					  }
				  },
				  error:function(data){
					  layer.msg(""+data.message+"", {icon: 5, time: 1000});
				  }
				});
				setTimeout('parent.location.reload();',1000);
		})
		
		//添加权限下拉框
		function findByCompetence(form){
			$.ajax({
				//请求方式
				type: "GET",
				//请求地址
				url: "http://localhost:8081/competence/findAll",
				dataType: "json",
				success: function (date) {
					if(date.code==0){
						var list = date.data;    //返回的数据
						var government = $("#updatName");//server为select定义的id
						for (var i = 0; i < list.length; i++) {
							var option = document.createElement("option");  // 创建添加option属性
							option.setAttribute("value", list[i].competenceId); // 给option的value添加值
							option.innerText = list[i].competenceName;   // 打印option对应的纯文本
							if(personnel.competenceName === list[i].competenceName){
								//设置默认值
								option.selected = true;
							}
							government.append(option);           //给select添加option子标签
						}
						form.render("select");            // 刷新select，显示出数据
					}else{
						 layer.msg("请求失败", {icon: 5, time: 1000});
					}
				}
			});
		}
		//添加部门下拉框
		function findByPositionId(form){
			$.ajax({
				//请求方式
				type: "GET",
				//请求地址
				url: "http://localhost:8081/position/findAll",
				dataType: "json",
				success: function (date) {
					if(date.code==0){
						var list = date.data;    //返回的数据
						var government = $("#updatName");//server为select定义的id
						for (var i = 0; i < list.length; i++) {
							var option = document.createElement("option");  // 创建添加option属性
							option.setAttribute("value", list[i].positionId); // 给option的value添加值
							option.innerText = list[i].positionName;   // 打印option对应的纯文本
							government.append(option);           //给select添加option子标签
							if(personnel.positionName === list[i].positionName){
								//设置默认值
								option.selected = true;
							}
							form.render("select");            // 刷新select，显示出数据
						}
					}else{
						 layer.msg("请求失败", {icon: 5, time: 1000});
					}
				}
			});
		}
		
		//根据权限名称查询权限信息
		function findCompetenceByName(positionName){
			var positionId = 0;
			$.ajax({
			  url: "http://localhost:8081/position/findByName",//请求地址
			  type: "POST",//可以用post或者get
			  async: false,
			  data:{
				"positionName":positionName
			  },//请求数据
			  success:function(data){
				  if(data.code==0){
					positionId = data[0].positionId;
				  }
			  },
			  error:function(data){
				 positionId = 0;
			  }
			});
			return positionId;
		}
	})